window.addEventListener('load',function() {

	var type 	= localStorage.getItem("estudia_type");
	var course	= localStorage.getItem("estudia_course");
	
	var param 		= location.search.split('page=')[1];
	
	$('#title').append("<h1>" + (localStorage.getItem("estudia_chapter")).slice(2) + "</h1>");
	/*
	$('#type').append(type);
	$('#course').append(course); // */
	
	if (type == "soal") {
		$('#resultset').hide();
		
		var file 	= "../assets/bank/" + type + "/" + course + "/bab 1/" + param + ".txt";
		var submit 	= document.getElementsByName('submit')[0];
		
		var jawaban;
		
		$('#rad_a').focus();
		
		var xhr = new XMLHttpRequest();
		xhr.open("GET", file, true);
		xhr.onreadystatechange = function ()
		{
			if(xhr.readyState === 4)
			{
				if(xhr.status === 200 || xhr.status == 0)
				{
					var response = JSON.parse(xhr.responseText);
					
					$('#question').append(param + ". " + response["soal"]);
					
					$('#pil_a').append(response["pil_a"]);
					$('#pil_b').append(response["pil_b"]);
					$('#pil_c').append(response["pil_c"]);
					$('#pil_d').append(response["pil_d"]);
					
					jawaban = response["jawaban"];
				}
			}
		}
		xhr.send(null);
		
		document.getElementById('answer').onsubmit = function() {
			var selected = $("input[type='radio'][name='answers']:checked");
			
			var jawaban_benar = "<h3>Jawaban kamu benar (point +20)</h3>";
			var jawaban_salah = "<h3>Jawaban kamu salah<br />Jawaban benar = " + jawaban + "</h3>";
			
			if (param == 5) {
				$('#next_button').append("Quiz Summary");
			} else {
				$('#next_button').append("Pertanyaan Selanjutnya");
			}
			
			$('.answers').attr("disabled", true);
			$('#resultset').show();
			
			//alert(selected.val() + " = " + jawaban);
			if (selected.val() == jawaban) {
				$('#answer_result').append(jawaban_benar);
				var right = parseInt(localStorage.getItem("estudia_ans_right")) + 1;
				localStorage.setItem("estudia_ans_right", right);
			} else {
				$('#answer_result').append(jawaban_salah);
				var wrong = parseInt(localStorage.getItem("estudia_ans_false")) + 1;
				localStorage.setItem("estudia_ans_false", wrong);
			}
			
			return false;
		};	
		
		$('#next_button').click(function() {
			if (param != 5) {
				window.location.href = 'tampilansoal.html?page=' + (parseInt(param) + 1);
			} else {
				//alert(localStorage.getItem("estudia_ans_right"));
				//alert(localStorage.getItem("estudia_ans_false"));
				
				window.location.href = 'quizsummary.html';
				
			}
		});
		
	} else if (type == "materi") {
		var num_chapter	= localStorage.getItem("estudia_maxpage");
		//alert(param);
		//alert(course);
		//alert(localStorage.getItem("estudia_maxpage"));
		
		$('#materi_next').click(function() {
			window.location.href = 'tampilanmateri.html?page=' + (parseInt(param) + 1);
			//alert(current);
		});
		
		$('#materi_prev').click(function() {
			window.location.href = 'tampilanmateri.html?page=' + (parseInt(param) - 1);
			//alert(current);
		});
		
		var path = "../assets/bank/materi/" + course + "/" + (localStorage.getItem("estudia_chapter")).charAt(0) + "-" + param + ".jpg";
		$('#lesson_image').append("<img src=\"" + path + "\"/>");
		
		if (param == 1) {
			$('#materi_next').show();
			$('#materi_prev').hide();
		} else if (param == num_chapter) {
			$('#materi_next').hide();
			$('#materi_prev').show();
		} else {
			$('#materi_next').show();
			$('#materi_prev').show();
		}		
	}
});